<template>
  <el-row>
    <el-col :span="24">
      {{ cardInfo.content }}
    </el-col>
    <el-col :span="24">
      <div v-if="cardInfo.noticeRoute">
        <el-button type="text" size="mini" @click="edit()">
          请查看详细信息
        </el-button>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import {
  update,
  detail
} from '@neu/biz-core/api/notice'

// 定义一个表单原始项
const cardInfo = {
  isRead: '',
  title: '',
  content: '',
  noticeType: '',
  businessInfo: ''
}
export default {
  name: 'NoticeDescForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      cardInfo: {
        ...cardInfo
      },
      modal: true
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.cardInfo = {
            ...response.data
          }
        })
      } else {
        this.cardInfo = {
          ...cardInfo
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              this.cardInfo.isRead = 'read'
              resolve(update(this.id, this.cardInfo))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    //  noticeType: [
    //     { value: 'basic', label: '基本通知' },
    //     { value: 'maintenanceExpire', label: '维保到期' },
    //     { value: 'assetExpire', label: '' },
    //     { value: 'workOrder', label: '' },
    //     { value: 'lifeCircle', label: '' },
    //     { value: 'inventory', label: '' },
    //     { value: 'assetLend', label: '' }
    //   ]
    // }
    edit() {
      // if (this.cardInfo.noticeType === 'maintenanceExpire') { // 维保到期
      //   this.$router.push({ name: 'dcam_fixedAsset', params: JSON.parse(this.cardInfo.businessInfo) })
      // } else if (this.cardInfo.noticeType === 'assetExpire') { // 资产到期
      //   this.$router.push({ name: 'Profile', params: JSON.parse(this.cardInfo.businessInfo) })
      // } else if (this.cardInfo.noticeType === 'workOrder') { // 工单
      //   // console.log(JSON.parse(this.cardInfo.businessInfo))
      //   this.$router.push({ name: 'Profile', params: JSON.parse(this.cardInfo.businessInfo) })
      // } else if (this.cardInfo.noticeType === 'lifeCircle') { // 生命周期
      //   this.$router.push({ name: 'Profile', params: JSON.parse(this.cardInfo.businessInfo) })
      // } else if (this.cardInfo.noticeType === 'inventory') { // 库存提醒
      //   this.$router.push({ name: 'Profile', params: JSON.parse(this.cardInfo.businessInfo) })
      // } else if (this.cardInfo.noticeType === 'assetLend') { // 资产借出
      //   this.$router.push({ name: 'Profile', params: JSON.parse(this.cardInfo.businessInfo) })
      // } else {
      //   // do nothing
      // }
      if (this.cardInfo.noticeRoute) {
        this.$router.push({ name: this.cardInfo.noticeRoute, params: JSON.parse(this.cardInfo.businessInfo) })
      }
    }
  }
}
</script>
